/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package FrontController;

import BDelegate.BussinesDelegate;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Damc
 */
public class ControlE extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession sesion = request.getSession();
        try {
            String peticion = request.getParameter("request");
            if(peticion==null){
                sesion.removeAttribute("tabla");
                List<String> a = new ArrayList<String>();
                BussinesDelegate bd = new BussinesDelegate();
                String id = (String) sesion.getAttribute("id");
                out.println(id);
                a=bd.ListaEmpresasdeUsuario(id);
                out.println(a);
                int filas=5;
                sesion.setAttribute("filas", filas);
                sesion.setAttribute("tabla", a);
                response.sendRedirect("indexE.jsp");
            }
            else{
                if(peticion.equals("inicio")){
                    sesion.removeAttribute("tabla");
                    response.sendRedirect("indexE.jsp");
                }
                if(peticion.equals("editar")){
                    sesion.removeAttribute("tabla");
                    response.sendRedirect("editarU.jsp");
                }
                if(peticion.equals("verEmpresa")){
                    String empresa="";
                    empresa = request.getParameter("empresa");
                    sesion.removeAttribute("tabla");
                    request.setAttribute("empresa", empresa);
                    BussinesDelegate bd = new BussinesDelegate();
                    VO.Empresas ve = bd.datosempresa(empresa);

                    request.setAttribute("descripcion", ve.getDescripcion());
                    request.setAttribute("direccion", ve.getDireccion());
                    request.setAttribute("mail", ve.getMail());
                    request.setAttribute("telefono", ve.getTelefono());
                    request.setAttribute("banner", ve.getURLbanner());
                    request.setAttribute("url", ve.getURLweb());
                    
                    //response.sendRedirect("editarE.jsp");
                    request.getRequestDispatcher("editarE.jsp").include(request, response);
                }
                if(peticion.equals("pagar")){
                    String empresa="";
                    empresa = request.getParameter("empresa");
                    sesion.removeAttribute("tabla");
                    BussinesDelegate bd = new BussinesDelegate();
                    List<String> tip= new ArrayList<String>();
                    try{
                    tip = bd.tipoPub();
                    }catch(Exception e){}
                    //out.print(tip);
                    request.setAttribute("empresa", empresa);
                    //request.setAttribute("tabla", tip);
                    sesion.setAttribute("tabla", tip);
                    //out.print(tip);
                    //response.sendRedirect("editarE.jsp");
                    request.getRequestDispatcher("pagar.jsp").include(request, response);
                }
                if(peticion.equals("crearE")){
                    sesion.removeAttribute("tabla");
                    response.sendRedirect("crearE.jsp");
                }
                if(peticion.equals("crearempresa")){
                    sesion.removeAttribute("tabla");
                    String idempresa = request.getParameter("nombre");
                    String mail = request.getParameter("mail");
                    String urlweb = request.getParameter("url");
                    String telefono = request.getParameter("telefono");
                    String direccion = request.getParameter("direccion");
                    String descripcion = request.getParameter("descripcion");

                    BussinesDelegate bd = new BussinesDelegate();
                    try{
                        boolean var=bd.datosnonulos(idempresa, mail, urlweb);
                        if(var){
                            VO.Empresas ve = new VO.Empresas(idempresa);
                            ve.setDescripcion(descripcion);
                            ve.setDireccion(direccion);
                            ve.setMail(mail);
                            ve.setTelefono(telefono);
                            ve.setURLweb(urlweb);
                            sesion.setAttribute("tabla", ve);
                            response.sendRedirect("subirBanner.jsp");
                        }else{
                            String message="ERROR: La empresa ya existe";
                            request.setAttribute("error", message);
                            request.getRequestDispatcher("crearE.jsp").include(request, response);
                        }
                    }catch(Exception e){
                        request.setAttribute("error", e.getMessage());
                        request.getRequestDispatcher("crearE.jsp").include(request, response);
                    }
                    
                    //response.sendRedirect("crearE.jsp");
                }
                if(peticion.equals("editarempresa")){
                    sesion.removeAttribute("tabla");
                    String idempresa = request.getParameter("empresa");
                    String mail = request.getParameter("mail");
                    String banner = request.getParameter("banner");
                    String urlweb = request.getParameter("url");
                    String telefono = request.getParameter("telefono");
                    String direccion = request.getParameter("direccion");
                    String descripcion = request.getParameter("descripcion");

                    BussinesDelegate bd = new BussinesDelegate();
                    String message="Se actualizo con exito los datos";
                    try{
                        boolean var=bd.datosnonulos2(idempresa, mail, urlweb);
                        if(var){
                            
                            String idusuario=(String) sesion.getAttribute("id");
                            bd.editarEmpresa(idusuario, idempresa, "0", banner, urlweb, telefono, mail, direccion, descripcion);
                            
                        }else{
                            message="ERROR: Error al editar la empresa";
                            
                        }
                        VO.Empresas ve1 = bd.datosempresa(idempresa);
                        request.setAttribute("empresa", idempresa);
                        request.setAttribute("descripcion", ve1.getDescripcion());
                        request.setAttribute("direccion", ve1.getDireccion());
                        request.setAttribute("mail", ve1.getMail());
                        request.setAttribute("telefono", ve1.getTelefono());
                        request.setAttribute("banner", ve1.getURLbanner());
                        request.setAttribute("url", ve1.getURLweb());
                        
                        request.setAttribute("error", message);
                            request.getRequestDispatcher("editarE.jsp").include(request, response);
                    }catch(Exception e){
                        VO.Empresas ve1 = bd.datosempresa(idempresa);
                        request.setAttribute("empresa", idempresa);
                        request.setAttribute("descripcion", ve1.getDescripcion());
                        request.setAttribute("direccion", ve1.getDireccion());
                        request.setAttribute("mail", ve1.getMail());
                        request.setAttribute("telefono", ve1.getTelefono());
                        request.setAttribute("banner", ve1.getURLbanner());
                        request.setAttribute("url", ve1.getURLweb());
                        request.setAttribute("error", e.getMessage());
                        request.getRequestDispatcher("editarE.jsp").include(request, response);
                    }
                    
                    //response.sendRedirect("crearE.jsp");
                }


            }
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ControlE</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ControlE at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } catch(Exception w){
            sesion.invalidate();
            response.sendRedirect("../inicio.jsp");
        }finally {
            out.close();
        }
    }

    public String geturl(){
        return "jojo";
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
